<?php
class Category extends AppModel {

	var $name = 'Category';

	//The Associations below have been created with all possible keys, those that are not needed can be removed
	var $hasMany = array(
			'Post' =>
				array('className' => 'Post',
						'foreignKey' => 'category_id',
						'conditions' => '',
						'fields' => '',
						'order' => '',
						'limit' => '',
						'offset' => '',
						'dependent' => '',
						'exclusive' => '',
						'finderQuery' => '',
						'counterQuery' => ''
				),

	);

	function postcount($cid,$dir) {
		$this->id = $cid;
        if ($dir)
          	$this->saveField('count', ($this->field('count')+1));
		else
			$this->saveField('count', ($this->field('count')-1));
	}

	function bestUsers($cid) {
		return $this->query('
select users.login, users.name, sum(comments.vote) as points from comments left join users on users.id=comments.user_id left join posts on comments.post_id=posts.id where posts.category_id='.$cid.' group by users.login having points>0 order by points desc LIMIT 10
		');
	}

	function catChildren($cid) {
				return $this->findAll('Category.parent_id = '.$cid,array("Category.id", "Category.name", "Category.description","Category.count","Category.modified"),"Category.order ASC",null,null,-1);
	}

}
?>